/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { getOrderWorkTopInfo, getWorkEchartsData } from "@/services/api";
import { message } from "antd";
import { isArray } from "lodash";

export default {
  namespace: "orderWorkTop",
  state: {
    workTopInfo: {},
    echartsData: {},
  },
  effects: {
    *getOrderWorkTopInfo({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOrderWorkTopInfo, payload);
      const { code, data = {} } = response;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            workTopInfo: data || {},
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
      }
    },
    *getWorkEchartsData({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getWorkEchartsData, payload);
      const { code, data } = response;
      let keys = {
        month: { num: "monthNum", overNum: "monthOverNum" },
        week: {
          num: "weekNum",
          overNum: "weekOverNum",
          recentlyList: "weekrecentlyList",
          recentlyLists: "weekrecentlyLists",
        },
      };
      const decs = {
        recentlyList: "订单总数",
        recentlyLists: "已完成订单",
      };
      for (let key in data) {
        let value = keys[key];
        let currentData = data[key];
        if (value) {
          for (let i in value) {
            currentData[i] = currentData[value[i]];
            delete currentData[value[i]];
          }
        }
        for (let j in currentData) {
          if (["recentlyList", "recentlyLists"].find(i => j.includes(i))) {
            const list = currentData["list"] || [];
            for (let lis in currentData[j]) {
              list.push({
                type: decs[j],
                value: parseFloat(currentData[j][lis]),
                time: lis,
              });
            }
            currentData["list"] = list;
          }
        }
      }
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            echartsData: data,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
